from PyQt5.QtWidgets import QMenu, QAction, QGraphicsView, QGraphicsScene, QFileDialog, QDialog, QGraphicsItem, \
    QGraphicsPixmapItem
from PyQt5.QtCore import pyqtSignal, Qt, QRectF, QEvent
from PyQt5.QtGui import QIcon, QImage, QPainter, QResizeEvent, QPixmap, QContextMenuEvent, QPaintEvent


class GraphicsItem(QGraphicsItem):

    def __init__(self, parent=None):
        super().__init__(parent)

    def paint(self, painter, style, widget=None):
        print('绘制', self)


class GraphicsPixmapItem(QGraphicsPixmapItem):
    compositionMode = QPainter.CompositionMode_SourceOver

    def __init__(self, pixmap, parent=None):
        super().__init__(pixmap, parent)

    def paint(self, painter, style, widget=None):
        painter.setCompositionMode(self.compositionMode)
        QGraphicsPixmapItem.paint(self, painter, style, widget)


class View(QGraphicsView):
    context: QMenu = None
    trackMouseMove = False
    imageWindow = None
    viewSize = None
    padding = None  # 上下/左右   内编剧

    def __init__(self, parent):
        super().__init__(parent)
        self.imageWindow = parent.parent()
        self.initUI()

    def initUI(self):
        # 锚点
        self.setTransformationAnchor(QGraphicsView.AnchorViewCenter)
        # padding
        self.padding = (10, 10)
        self.setBackgroundBrush(Qt.transparent)

    def initContext(self):
        # Content
        view = self.View
        menu = QMenu(view)

        Act = QAction("缩放", menu)
        menu.addAction(Act)
        Act.triggered.connect(self._pasteContext)
        menu.newAct = Act

        deleteAct = QAction("删除", menu)
        menu.addAction(deleteAct)
        deleteAct.triggered.connect(self._removeContextLayer)
        menu.deleteAct = deleteAct

        menu.addSeparator()

    def mousePressEvent(self, event):
        '''
        鼠标点击视图
        :param event: QMouseEvent
        :return:
        '''
        if event.button() == Qt.LeftButton:  # 左键
            # 工具方法

            window = self.imageWindow.window  # 主窗体
            tool = window.toolBar.currentTool
            if hasattr(tool, 'mousePressGraphicsViewEventCallBack'):
                print('点击view:', tool.name)
                tool.mousePressGraphicsViewEventCallBack(tool, event)
        QGraphicsView.mousePressEvent(self, event)

    def mouseMoveEvent(self, event):
        '''
        视图中移动鼠标
        :param event:
        :return:
        '''
        if self.trackMouseMove:
            # print('view 鼠标移动')
            # 工具方法
            window = self.imageWindow.window  # 主窗体
            tool = window.toolBar.currentTool
            # print(tool.name)
            if hasattr(tool, 'mouseMoveGraphicsViewEventCallBack'):
                tool.mouseMoveGraphicsViewEventCallBack(tool, event)
        QGraphicsView.mouseMoveEvent(self, event)

    def mouseReleaseEvent(self, event):
        '''
        鼠标释放视图
        :param event:
        :return:
        '''
        if event.button() == Qt.LeftButton:  # 左键
            # print('view 鼠标释放')
            # 工具方法
            window = self.imageWindow.window  # 主窗体
            tool = window.toolBar.currentTool
            if hasattr(tool, 'mouseReleaseGraphicsViewEventCallBack'):
                print('释放view:', tool.name)
                tool.mouseReleaseGraphicsViewEventCallBack(tool, event)
        QGraphicsView.mouseReleaseEvent(self, event)

    def updatePos(self):
        size = self.imageWindow.canvasSize
        if not size:
            return
        w = size.width() + self.padding[1]
        h = size.height() + self.padding[0]
        pw = self.parent().width()
        ph = self.parent().height()

        if w > pw - self.padding[1]:
            w = pw - self.padding[1]
        if h > ph - self.padding[0]:
            w = ph - self.padding[0]
        # 移动到中心
        self.setGeometry((pw - w) / 2, (ph - h) / 2, w, h)

    def contextMenuEvent(self, event: QContextMenuEvent):
        print('contextMenu of View')

    def event(self, event):
        '''
        if event.type() == QEvent.ContextMenu:
            self.contextMenuEvent(event)
        '''
        return QGraphicsView.event(self, event)

    '''

    def paintEvent(self, event):
        scene = self.scene()
        items = scene.
        pass
     '''


class ImageDialog(QDialog):
    pixmap = None  # 图像数据

    def __init__(self, parent=None, pixmap=None):
        """
        显示图像的Widget
        :param parent:
        """
        super().__init__(parent)
        self.setAttribute(Qt.WA_DeleteOnClose)
        if pixmap:
            self.pixmap = pixmap
            self.resize(pixmap.size())

    def paintEvent(self, event: QPaintEvent):
        if self.pixmap is None:
            return
        qp = QPainter()
        qp.begin(self)
        qp.drawPixmap(self.rect(), self.pixmap, self.pixmap.rect())
        qp.end()


